﻿using System;
using System.Collections;
using System.Web.UI;
using RealEstate.Models;
using Telerik.Web.UI;

namespace RealEstate.Views.Admin
{
    public partial class ManageClientType : Page
    {
        private ClientTypeEntities _dbContext;
        public ClientTypeEntities DbContext
        {
            get { return _dbContext ?? (_dbContext = new ClientTypeEntities()); }
        }

        protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            RadGrid1.DataSource = DbContext.GetAll();
        }

        protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            var item = e.Item as GridEditableItem;
            if (item != null)
            {
                var id = item.GetDataKeyValue("Id").ToString();
                var clientType = DbContext.GetById(Convert.ToInt32(id));
                item.UpdateValues(clientType);
                DbContext.Update(clientType);
            }
        }

        protected void RadGrid1_DeleteCommand(object sender, GridCommandEventArgs e)
        {
            var id = ((GridDataItem)e.Item).GetDataKeyValue("Id").ToString();
            DbContext.Delete(Convert.ToInt32(id));
        }

        protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e)
        {
            var item = e.Item as GridEditableItem;
            var values = new Hashtable();
            var clientType = new ClientType();
            if (item != null)
            {
                item.ExtractValues(values);
                item.UpdateValues(clientType);
                if (!string.IsNullOrEmpty(clientType.ClientType1))
                    DbContext.Add(clientType);
                else
                    return;
            }
        }
    }
}